package com.enterpriseDigitalPlatform.controller.index;

import com.enterpriseDigitalPlatform.anotation.NoAuth;
import com.enterpriseDigitalPlatform.dto.JsonRes;
import com.enterpriseDigitalPlatform.util.IpUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/")
public class IndexC {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/")
    @NoAuth
    public Object index(HttpServletRequest request) {
        // String sql = "select * from test where id > 9 limit 1";
        // Test list = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Test.class));
        // String sql = "select * from test where id > 9";
        // List<Test> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Test.class));
        // List<Test> list = testService.testList();
        String sql = "SELECT * FROM test WHERE id > 9 LIMIT 1";
        List<Test> resultList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Test.class));
        Test list = resultList.isEmpty() ? null : resultList.get(0);
        return new JsonRes<>(200, IpUtil.getIpAddr(request), list);
    }

    @Autowired
    private TestService testService;


    @RequestMapping("/testAdd")
    @NoAuth
    public Object testAdd(HttpServletRequest request) {
        Test test = new Test();
        test.setName("testAdd");
        long row = testService.testAdd(test);
        return new JsonRes<>(200, row + "", test);
    }

}
